package com.zz.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zz.common.Log;
import com.zz.common.R;
import com.zz.entity.*;
import com.zz.service.RolePermissionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Nullable;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 角色权限管理
 * @author 朱彦燕
 */
@RestController
@RequestMapping("/rolePermission")
@Slf4j
@CrossOrigin
@Validated
public class RolePermissionController {

    @Autowired
    private RolePermissionService rolePermissionService;

    /**
     * 按条件 分页 展示所有角色权限信息
     * 朱彦燕
     * 测试成功
     * */
    @RequiresRoles("admin")
    @GetMapping("/page")
   // @Cacheable(value="permissionCache",key="'permission'+#page+#pageSize+#roleRealName+#permissionName",sync = true)
    @Log(title = "RBAC:按条件 分页 展示所有角色权限信息", dataLocationType = "link")
    public R<Page> pageRolePermission(@RequestParam @NotNull(message = "起始页码不能为空") int page,
                        @RequestParam @NotNull(message = "每页最大值不能为空") int pageSize,
                        @RequestParam @Nullable String roleRealName,
                        @RequestParam @Nullable String permissionName){
        return rolePermissionService.page(page, pageSize, roleRealName, permissionName);
    }

    /**
     * 增加角色权限
     * @param rolePermission
     * 朱彦燕
     * 测试成功
     * */
    @RequiresRoles("admin")
    @PostMapping("/add")
    @CacheEvict(value = "permissionCache",allEntries = true)
    @Log(title = "RBAC:增加角色权限", dataLocationType = "body")
    public R<String> addRolePermission(@RequestBody @Validated(Insert.class) RolePermission rolePermission){
        return rolePermissionService.add(rolePermission);
    }

    /**
     * 删除角色权限
     * @param rolePermission
     * 朱彦燕
     * 测试成功
     * */
    @RequiresRoles("admin")
    @DeleteMapping("/delete")
    @CacheEvict(value = "permissionCache",allEntries = true)
    @Log(title = "RBAC:删除角色权限", dataLocationType = "body")
    public R<String> deleteRolePermission(@RequestBody @Validated(Delete.class) RolePermission rolePermission){
        return rolePermissionService.delete(rolePermission);
    }

    /**
     * @param rolePermission
     * 修改角色权限
     * 朱彦燕
     * 测试成功
     * */
    @RequiresRoles("admin")
    @PutMapping("/update")
    @CacheEvict(value = "permissionCache",allEntries = true)
    @Log(title = "RBAC:修改角色权限", dataLocationType = "body")
    public R<String> updateRolePermission(@RequestBody @Valid RolePermission rolePermission){
        return rolePermissionService.update(rolePermission);
    }

}
